package com.temi.demo.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.temi.demo.R;

public class CustomToolbar extends Toolbar {

    private final Context mContext;

    private View mView;
    private RelativeLayout mLeftParent;
    private ImageView mLeftIcon;
    private TextView mLeftText;
    private TextView mMiddleText;
    private int textColor;

    public CustomToolbar(Context context) {
        this(context, null);
    }

    public CustomToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();

        if (attrs != null) {

            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomToolbar);

            textColor = a.getColor(R.styleable.CustomToolbar_textColor, ContextCompat.getColor(mContext, R.color.white));

            final Drawable leftIcon = a.getDrawable(R.styleable.CustomToolbar_leftIcon);
            if (leftIcon != null) {
                setLeftIcon(leftIcon);
            }

            CharSequence leftText = a.getText(R.styleable.CustomToolbar_leftText);
            if (!TextUtils.isEmpty(leftText)) {
                setLeftText(leftText);
            }

            CharSequence middleText = a.getText(R.styleable.CustomToolbar_middleText_only);
            if (!TextUtils.isEmpty(middleText)) {
                setMiddleText(middleText);
            }

            a.recycle();
        }

    }

    private void initView() {
        if (mView == null) {

            LayoutInflater mInflater = LayoutInflater.from(getContext());
            mView = mInflater.inflate(R.layout.toolbar, null);

            mLeftParent = mView.findViewById(R.id.toolbar_rl_left_parent);
            mLeftIcon = mView.findViewById(R.id.toolbar_left_icon);
            mLeftText = mView.findViewById(R.id.toolbar_left_text);
            mMiddleText = mView.findViewById(R.id.toolbar_middle_text);

            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
            addView(mView, lp);
        }
    }

    public void setLeftIcon(Drawable icon) {
        if (mLeftIcon != null) {
            mLeftIcon.setBackgroundDrawable(icon);
            if (mLeftIcon.getVisibility() != View.VISIBLE) {
                mLeftIcon.setVisibility(VISIBLE);
            }
        }
    }

    public void setLeftIcon(int icon) {
        setLeftIcon(getResources().getDrawable(icon));
    }

    public ImageView getRightIconView() {
        if (mLeftIcon.getVisibility() != View.VISIBLE) {
            mLeftIcon.setVisibility(VISIBLE);
        }
        return mLeftIcon;
    }

    public void setLeftText(int id) {
        String str = getResources().getString(id);
        setLeftText(str);
    }

    public void setLeftText(CharSequence text) {
        mLeftText.setText(text);
        mLeftText.setTextColor(textColor);
        mLeftText.setPadding(dp2px(mContext, 7), 0, 0, 0);
        if (mLeftText.getVisibility() != View.VISIBLE) {
            mLeftText.setVisibility(VISIBLE);
        }
    }

    public TextView getLeftText() {
        if (mLeftText.getVisibility() != View.VISIBLE) {
            mLeftText.setVisibility(VISIBLE);
        }
        return mLeftText;
    }


    public void setLeftPartClickListener(OnClickListener listener) {
        mLeftParent.setOnClickListener(listener);
    }

    public void setMiddleText(int id) {
        String str = getResources().getString(id);
        setMiddleText(str);
    }

    public void setMiddleText(CharSequence middleText) {
        mMiddleText.setText(middleText);
        mMiddleText.setTextColor(textColor);
        if (mMiddleText.getVisibility() != View.VISIBLE) {
            mMiddleText.setVisibility(VISIBLE);
        }
    }

    public TextView getLeftTextView() {
        if (mMiddleText.getVisibility() != View.VISIBLE) {
            mMiddleText.setVisibility(VISIBLE);
        }
        return this.mLeftText;
    }

    @Override
    public void setTitle(int resId) {
        super.setTitle(resId);
    }

    public static int dp2px(Context context, int dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}